<?php
include("model/customers.php");
class customersController{
	
	function indexAction(){
		$smarty = $_SESSION['smarty'];
		
		$list_customers_box = $this->getListCustomerBox();
		$smarty->assign("list_customers_box",$list_customers_box);
		
		$content = $smarty->fetch("customers/customers.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function getListCustomerBox(){
		$smarty 	= $_SESSION['smarty'];
		$customer_obj 	= new customers();
		$customerList	= $customer_obj->getCustomersList();		
		$smarty->assign("customerList", $customerList);
		$html 		= $smarty->fetch("customers/customers_list.html");
		return $html;
	}
	
	function insertAction(){
		$customers_obj 	= new customers();
		$data 			= array();
		$data['cus_firstname'] 	= $_REQUEST['cus_firstname'];
		$data['cus_lastname'] 	= $_REQUEST['cus_lastname'];
		$data['cus_address']    = $_REQUEST['cus_address'];
		$data['cus_phone'] 		= $_REQUEST['cus_phone'];
		
		$result = $customers_obj->insertACustomer($data);
		if ($result == 1)
			$msg = "insert successfully";
		else if ($result == -1)
			$msg = "duplicated!";
		else
			$msg = "insert failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("customers/customers_insertResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
		
	}
	
	function inactiveAction(){
		$cus_id = $_REQUEST['args'];
		$customers_obj = new customers();
		$result = $customers_obj->inactivateACustomer($cus_id);
		if ($result == 1)
			$msg = "inactivate successfully";		
		else
			$msg = "inactivate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_customers_box = $this->getListCustomerBox();
		$smarty->assign("list_customers_box",$list_customers_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("customers/customers.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function activeAction(){
		$cus_id = $_REQUEST['args'];
		$customers_obj = new customers();
		$result = $customers_obj->activateACustomer($cus_id);
		if ($result == 1)
			$msg = "activate successfully";		
		else
			$msg = "activate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_customers_box = $this->getListCustomerBox();
		$smarty->assign("list_customers_box",$list_customers_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("customers/customers.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function insertFormAction(){
		$smarty = $_SESSION['smarty'];		
		
		$insertform_customers_box = $smarty->fetch("customers/customers_insert_form.html");
		$smarty->assign("insertform_customers_box",$insertform_customers_box);
		
		$content = $smarty->fetch("customers/customers.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function editFormAction(){
		$cus_id 			= $_REQUEST['args'];
		$smarty 			= $_SESSION['smarty'];		
		
		$customers_obj 		= new customers();
		$current_data 		= array();
		$current_data 		= $customers_obj->getCustomersInfo($cus_id);
		
		$smarty->assign("current_data", $current_data);
		
		$editform_customers_box = $smarty->fetch("customers/customers_edit_form.html");
		$smarty->assign("editform_customers_box",$editform_customers_box);
		
		$content = $smarty->fetch("customers/customers.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
		
	}
	
	function editAction(){
		$customers_obj = new customers();
		$data 		= array();
		
		$data['cus_id'] 		= $_REQUEST['cus_id'];
		$data['cus_firstname'] 	= $_REQUEST['cus_firstname'];
		$data['cus_lastname'] 	= $_REQUEST['cus_lastname'];
		$data['cus_address'] 	= $_REQUEST['cus_address'];
		$data['cus_phone'] 		= $_REQUEST['cus_phone'];
		
		
		$result = $customers_obj->editACustomer($data);
		
		if ($result == 1)
			$msg = "edit successfully";
		else if ($result == -1)
			$msg = "duplicated!";
		else
			$msg = "update failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("customers/customers_updateResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
}
?>